<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class DataContentGrabProject extends AdminBase
{
    private $table = 'data_content_grab_project';
    private $table_data = 'data_content_grab_project_data';

    public function index()
    {
        $param = $this->request->param();
        $db = Db::name($this->table);
        if(!empty($param['parent_typeid'])) {
            $db = $db->where('parent_typeid',$param['parent_typeid']);
            if(!empty($param['typeid'])){
                $db = $db->where('typeid',$param['typeid']);
            }
        }
        if(!empty($param['kw'])){
            $db = $db->where('title','like','%'.$param['kw'].'%');
        }
        $list = $db->order('id desc')->field('id,title,typeid,parent_typeid,project,source,channel,create_time')->paginate(['list_rows'=> 100,'query' => $param]);
        $count = $list->total();
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $typelist = Db::name('data_type')->where([
            'level' => 2,
            'pid' => $param['parent_typeid']
        ])->select()->toArray();
        return view('data_content_grab/index',['list'=>$list,'count'=>$count,'param'=>$param,'parent_typelist'=>$parent_typelist,'typelist'=>$typelist]);
    }

    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $param['channel'] = 2;
            if(!empty($param['parent_typeid'])) $param['top_typeid'] = get_typepid($param['parent_typeid']);
            $post = $param['post'];
            unset($param['post']);
            $insertId = Db::name($this->table)->insertGetId($param);
            if( $insertId ) {
                $post['id'] = $insertId;
                if(!empty($post['attr_ids'])) $post['attr_ids'] = implode(',',$post['attr_ids']);
                if(!empty($post['crowd_ids'])) $post['crowd_ids'] = implode(',',$post['crowd_ids']);
                if(!empty($post['amount_ids'])) $post['amount_ids'] = implode(',',$post['amount_ids']);
                if(!empty($post['region_ids'])) $post['region_ids'] = implode(',',$post['region_ids']);
                if(!empty($post['operate_ids'])) $post['operate_ids'] = implode(',',$post['operate_ids']);
                Db::name($this->table_data)->insertGetId($post);
                xn_add_admin_log('添加采集数据');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }    
        }
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $attrlist = Db::name('data_tag_attr')->select()->toArray();
        $crowdlist = Db::name('data_tag_crowd')->select()->toArray();
        $amountlist = Db::name('data_tag_amount')->select()->toArray();
        $regionlist = Db::name('data_tag_region')->select()->toArray();
        $operatelist = Db::name('data_tag_operate')->select()->toArray();

        return view('data_content_grab/form',[
            'parent_typelist'=>$parent_typelist,
            'attrlist'=>$attrlist,
            'crowdlist'=>$crowdlist,
            'amountlist'=>$amountlist,
            'regionlist'=>$regionlist,
            'operatelist'=>$operatelist
        ]);
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if(!empty($param['parent_typeid'])) $param['top_typeid'] = get_typepid($param['parent_typeid']);
            $post = $param['post'];
            unset($param['post']);
            
            Db::name($this->table)->update($param);
            if(!empty($post['attr_ids'])) {
                $post['attr_ids'] = implode(',',$post['attr_ids']);
            }else{
                $post['attr_ids'] = '';
            }
            if(!empty($post['crowd_ids'])) {
                $post['crowd_ids'] = implode(',',$post['crowd_ids']);
            }else{
                $post['crowd_ids'] = '';
            }
            if(!empty($post['amount_ids'])) {
                $post['amount_ids'] = implode(',',$post['amount_ids']);
            }else{
                $post['amount_ids'] = '';
            }
            if(!empty($post['region_ids'])) {
                $post['region_ids'] = implode(',',$post['region_ids']);
            }else{
                $post['region_ids'] = '';
            }
            if(!empty($post['operate_ids'])) {
                $post['operate_ids'] = implode(',',$post['operate_ids']);
            }else{
                $post['operate_ids'] = '';
            }
            Db::name($this->table_data)->where('id',$param['id'])->update($post);
            xn_add_admin_log('修改采集数据');
            $this->success('操作成功');
        }
        $id = $this->request->get('id');
        $data = Db::name($this->table)->find($id);
        $data['post'] = Db::name($this->table_data)->find($id);
        $data['post']['attr_ids'] = explode(',',$data['post']['attr_ids']);
        $data['post']['crowd_ids'] = explode(',',$data['post']['crowd_ids']);
        $data['post']['amount_ids'] = explode(',',$data['post']['amount_ids']);
        $data['post']['region_ids'] = explode(',',$data['post']['region_ids']);
        $data['post']['operate_ids'] = explode(',',$data['post']['operate_ids']);

        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $typelist = Db::name('data_type')->where([
            'level' => 2,
            'pid' => $data['parent_typeid']
        ])->select()->toArray();
        $attrlist = Db::name('data_tag_attr')->select()->toArray();
        $crowdlist = Db::name('data_tag_crowd')->select()->toArray();
        $amountlist = Db::name('data_tag_amount')->select()->toArray();
        $regionlist = Db::name('data_tag_region')->select()->toArray();
        $operatelist = Db::name('data_tag_operate')->select()->toArray();

        return view('data_content_grab/form',[
            'data'=>$data,
            'parent_typelist'=>$parent_typelist,
            'typelist'=>$typelist,
            'attrlist'=>$attrlist,
            'crowdlist'=>$crowdlist,
            'amountlist'=>$amountlist,
            'regionlist'=>$regionlist,
            'operatelist'=>$operatelist
        ]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        Db::name($this->table)->delete($id);
        Db::name($this->table_data)->delete($id);
        xn_add_admin_log('删除采集数据');
        $this->success('删除成功');
    }

    public function deleteAll(){
        $ids = $this->request->get('ids');
        ($ids=='') && $this->error('参数错误');
        $ids = explode(',',$ids);
        Db::name($this->table)->delete($ids);
        Db::name($this->table_data)->delete($ids);
        xn_add_admin_log('批量删除采集数据');
        $this->success("删除成功");
    }

}
